package handler

import (
	"car-order-mgr/cache"
	"car-order-mgr/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	redis_key := "car_order_detail_" + id
	var list model.CarOrderList
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	/*
		SELECT
		  car_orders.id OrderId,
		  cars.car_name CarName,
		  cars.car_type CarType,
		  car_orders.order_no OrderNo,
		  car_orders.order_price OrderPrice
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.cid
		WHERE car_orders.id=1
	*/
	if cacheData == "" {
		model.DB.Table("car_orders").
			Select(
				"car_orders.id OrderId",
				"cars.car_name CarName",
				"cars.car_type CarType",
				"car_orders.order_no OrderNo",
				"car_orders.order_price OrderPrice",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Where("car_orders.id=?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "详情查询成功",
		"data": list,
	})
	return
}

func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var total int64
	model.DB.Model(&model.CarOrder{}).Count(&total)
	var list []model.CarOrderList
	redis_key := "car_order_list_" + strconv.Itoa(page)
	/*
		SELECT
		  car_orders.id OrderId,
		  cars.car_name CarName,
		  cars.car_type CarType,
		  car_orders.order_no OrderNo,
		  car_orders.order_price OrderPrice
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.cid
	*/
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_orders").
			Select(
				"car_orders.id OrderId",
				"cars.car_name CarName",
				"cars.car_type CarType",
				"car_orders.order_no OrderNo",
				"car_orders.order_price OrderPrice",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Offset(offset).Limit(size).Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"total": total,
	})
	return
}

func CarOrderAdd(c *gin.Context) {
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	order_no := c.PostForm("order_no")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	data := model.CarOrder{
		Cid:        cid,
		OrderNo:    order_no,
		OrderPrice: order_price,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarAdd(c *gin.Context) {
	car_name := c.PostForm("car_name")
	car_type := c.PostForm("car_type")
	if car_name == "" || car_type == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Car{
		CarName: car_name,
		CarType: car_type,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
